package com.alibaba.cloud.encry;

import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

import javax.crypto.Cipher;

import org.apache.commons.codec.binary.Hex;


/**
 * The Class RsaUtils.
 *
 * @ClassName: RsaUtils
 * @Description: rsa 非对称 加密方式
 * @author: zhb zhb1316783262@163.com
 * @date: 2018年3月13日 下午2:39:05
 */
public class RsaUtils {
    
    /** The rsa public key. */
    private static RSAPublicKey rsaPublicKey;
    
    /** The rsa private key. */
    private static RSAPrivateKey rsaPrivateKey;

    /**
     * Encode.
     *
     * @param src the src
     * @return the string
     * @Description: 加密
     * @author: zhb zhb1316783262@163.com
     * @date: 2018年3月13日 下午2:38:55
     */
    public static String encode(String src) {
        try {
            // 初始化密钥
            KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
            keyPairGenerator.initialize(512);
            KeyPair keyPair = keyPairGenerator.generateKeyPair();
            // 生成公钥
            rsaPublicKey = (RSAPublicKey) keyPair.getPublic();
            // 生成私钥
            rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();

            // 私钥加密 公钥解密
            PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(rsaPrivateKey.getEncoded());
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.ENCRYPT_MODE, privateKey);
            byte[] resultBytes = cipher.doFinal(src.getBytes());

            // 私钥解密 公钥加密
            // X509EncodedKeySpec x509EncodedKeySpec =
            // new X509EncodedKeySpec(rsaPublicKey.getEncoded());
            // KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            // PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
            // Cipher cipher = Cipher.getInstance("RSA");
            // cipher.init(Cipher.ENCRYPT_MODE, publicKey);
            // byte[] resultBytes = cipher.doFinal(src.getBytes());

            return Hex.encodeHexString(resultBytes);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * Decode.
     *
     * @param src the src
     * @return the string
     */
    public static String decode(String src) {
        try {
            // 私钥加密 公钥解密
            X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(rsaPublicKey.getEncoded());
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.DECRYPT_MODE, publicKey);
            byte[] resultBytes = cipher.doFinal(Hex.decodeHex(src.toCharArray()));

            // 私钥解密 公钥加密
            // PKCS8EncodedKeySpec pkcs8EncodedKeySpec
            // = new PKCS8EncodedKeySpec(rsaPrivateKey.getEncoded());
            // KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            // PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
            // Cipher cipher = Cipher.getInstance("RSA");
            // cipher.init(Cipher.DECRYPT_MODE, privateKey);
            // byte[] resultBytes = cipher.doFinal(Hex.decodeHex(src.toCharArray()));

            return new String(resultBytes);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}